// toast
var Toast = function () {
    this.animating = false;
    this.imgUrl = {
        success: '/management/booking_hotel_success.svg',
        warn: '/new_tts/tk_ts_icon_new.svg'
    };
};
Toast.prototype = {
    show: function (type, str, showTime) {
        if(this.animating) return;
        if (!type) { console.error('type: success|warn'); return; }
        if (!str) { console.error('请填写提醒文字'); return; }
        if($('.hq_toast').length > 0) {

        } else {
            $('body').append('<div class="hq_toast fix tac bg-fff radius-5"><div class="clearfix dib"><img class="toast_img fl" src="https://img.haoqiao.com/website/images' + this.imgUrl[type] + '" alt=""/><p class="toast_txt f14 t-333 fl">' + str + '</p></div></div>');
        }
        $('.hq_toast').fadeIn('slow');
        var _this = this;
        if (showTime) {
            _this.animating = true;
            var hqTimeout = setTimeout(function () {
                _this.animating = false;
                $('.hq_toast').fadeOut();
                clearTimeout(hqTimeout);
            }, showTime);
        }
    },
    hide: function () {
        $('.hq_toast').fadeOut();
    }
};
var hq_toast = new Toast();

function set_cookie(name,value,expire) {
    var exp = new Date();
    exp.setTime(exp.getTime() + expire);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function get_cookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null)
        return unescape(arr[2]);
    return null;
}
function del_cookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = get_cookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
function wait_time(obj,sendwait) {
    if (sendwait == 0) {
        obj.removeClass("account-f-getcode-no");
        obj.html(lang_header.login_15);
        del_cookie('wait_time');
    } else {
        set_cookie('wait_time',sendwait);
        obj.addClass("account-f-getcode-no");
        obj.html(sendwait+HQ.config.language.com_errorApply[language][5]);
        sendwait--;
        setTimeout(function() {
                wait_time(obj,sendwait)
            },
            1000);
    }
}

$(function(){

    var salt = "";
	var reserved_salt = "";
    var is_login = 0;
    var J_nav_menu_flag = false;
    var current_page = window.location.href;
    $('#J_room_sel_win_nationality,.J_room_sel_win_peo').click(function () {
        return false;
    });
    $("#J_after_sale").click(function () {
        HQ.util.dialog.show({
            type: 'afterSale',
            icon: 'icon_popup_after_sale',
            title: dialogTxt.afterSale.title,
            main: dialogTxt.afterSale.main,
            content: dialogTxt.afterSale.content,
            confirmBtnText: '关闭',
        });
    });

    $('body').on('click', '.J_dialog_cancel', function () {
        HQ.util.dialog.hide();
    });

    $("#bg").click(function(){
        if($(this).hasClass("forbid_click")){
            return false;
        }
        $(".index-popup").fadeOut("slow");
        $("#bg").fadeOut("slow");
        HQ.util.dialog.hide();
    });
    
    $(".J_close_popup, #J_popup_after_sale_btn").click(function () {
        $(".index-popup").fadeOut("slow");
        $("#bg").fadeOut("slow");
        return false;
    });

    $(".J_close_popup_login_pop").click(function(){
        $(".index-popup").fadeOut("slow");
        $("#bg").fadeOut("slow");
        return false;
    });
    $(".J_pop_login").live("click", function () {
        var poition = HQ.util.position.centerPosition($("#J_popup_login"));
        $("#J_popup_login").css({left: poition.left, top: poition.top});
        $("#J_popup_login").fadeIn("slow");
        HQ.util.show_bg();
        $(".account-btn").attr("data-position","login_pop");
    });
    $(".J_price_pop_login").live("click", function () {
        var poition = HQ.util.position.centerPosition($("#J_popup_login"));
        $("#J_popup_login").css({left: poition.left, top: poition.top});
        $("#J_popup_login").fadeIn("slow");
        HQ.util.show_bg();
        $(".account-btn").attr("data-position","hotelprice_pop");
        $(".J_go_register").attr("data-position","hotelprice_pop");
        $(".J_go_personal_register").attr("data-position","hotelprice_pop");
        tj && tj.hotelprice_login_pop_click("hotelprice_login_pop\t"+encodeURIComponent(current_page));
    });

    // 公共登陆弹窗->B端注册账号、C端忘记密码
    $(".J_go_register,.J_go_personal_register").click(function(){
        if ($(this).attr('data-position') === 'hotelprice_pop') {
            tj && tj.hotelprice_login_pop_click("hotelprice_login_pop_register_2c\t"+encodeURIComponent(current_page));
        }
        window.open($(this).attr('data-href'));
    });

    $("#J_popup_tab li").click(function(){
        var index = $("#J_popup_tab li").index(this);
        $(".J_popup_tab_cont").hide().eq(index).show();
        $("#J_popup_error_msg").html('');
        $(this).addClass("current").siblings().removeClass("current");
        $(".account-f-l-txt").removeClass("login-txt-error");
        event.stopPropagation();
    });
    $(".account-f-l-radio").change(function(){
        var login_type = $(":radio[name='popup_login_type']:checked").val();
        $(".J_popup_tab_cont_t").hide();
        $(".J_"+login_type).show();
        $("#J_popup_error_msg").html('');
        $(".account-f-l-txt").removeClass("login-txt-error");
        event.stopPropagation();
    });

    // hotelmap弹出框登录注册
    $("#J_popup_code").focus(function(){
        var val = $(this).val();

        if(val == lang_header.login_3){
            $(this).val("");
        }
    }).keyup(function(){
        var val = $(this).val();

        if(val != "" && val != lang_header.login_3){
            $(this).css({color : "#333"});
        }else{
            $(this).css({color : "#999"});
        }
    }).blur(function(){
        var val = $(this).val();

        if(val.replace(/\s*/, "") == ""){
            $(this).val(lang_header.login_3);
        };
        $("#J_popup_code").removeClass("login-txt-error");
    }).keyup(function(ev){
        var val = $(this).val();
        if(ev.keyCode == 13 && is_login != 1 && val.replace(/\s*/, "") != ""){
            popup_login();
        }
    });

    $("#J_popup_name").focus(function(){
        var val = $(this).val();

        if(val == lang_header.login_4){
            $(this).val("");
        }
    }).keyup(function(){
        var val = $(this).val();

        if(val != "" && val != lang_header.login_4){
            $(this).css({color : "#333"});
        }else{
            $(this).css({color : "#999"});
        }
    }).blur(function(){
        var val = $(this).val();
        var code = $("#J_popup_code").val();
        if(code === lang_header.login_3 || code.replace(/\s*/, "") === ""){
            $("#J_popup_error_msg").html(lang_header.login_tips_1);
            $("#J_popup_code").focus().addClass("login-txt-error");
            return false;
        }else{
            $("#J_popup_code").removeClass("login-txt-error");
            $("#J_popup_error_msg").html("");
        }
        if(val.replace(/\s*/, "") == ""){
            $(this).val(lang_header.login_4);
        }else{
            HQ.ajax("/login/get_salt/", {username : val, code : code}, function(data){
                if(data.state == 1){
                    salt = data.data;
					reserved_salt = data.reserved_data;
                    $("#J_popup_error_msg").html("");
                    $("#J_popup_name").removeClass("login-txt-error");
                    $("#J_popup_code").removeClass("login-txt-error");
                }else{
                    //$("#J_popup_error_msg").html(data.msg);
                    $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.msg);
                }
            }, function(){}, "POST");
        }
    }).keyup(function(ev){
        var val = $(this).val();
        if(ev.keyCode == 13 && is_login != 1 && val.replace(/\s*/, "") != ""){
            popup_login();
        }
    });

    $("#J_popup_paw_txt").focus(function(){
        var val = $(this).val();

        $("#J_popup_paw").show().focus();
        $(this).hide();
    });
    $("#J_popup_paw").blur(function(){
        var val = $(this).val();

        if(val.replace(/\s*/, "") == ""){
            $("#J_popup_paw_txt").show();
            $(this).hide();
        }
    }).keyup(function(ev){
        var val = $(this).val();
        if(ev.keyCode == 13 && is_login != 1 && val.replace(/\s*/, "") != ""){
            popup_login();
        }
    });

    //登录按钮
    $("#J_popup_login_btn").click(function(){
        if(is_login == 1){
            return false;
        }
        popup_login();
    });

    function popup_login(){
        popup_check_login();
    }
    function popup_check_login(){
        $("#J_popup_login").val(lang_header.login_10);

        var code = $("#J_popup_code").val();
        var username = $("#J_popup_name").val();
        var paw = $("#J_popup_paw").val();
        var login_position = $(".account-btn").attr("data-position");

        if($('input[name="is_popup_remember"]').is(":checked")){
            var is_remember = 1;
        }else{
            var is_remember = 0;
        }

        if(code == lang_header.login_3 || code.replace(/\s*/, "") == ""){
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_1);
            $("#J_popup_code").focus().addClass("login-txt-error");

            $("#J_popup_login").val(lang_header.login_6);
            return false;
        }else{
            $("#J_popup_code").removeClass("login-txt-error");
            $("#J_popup_error_msg").html("");
        }
        if(username == lang_header.login_4 || username.replace(/\s*/, "") == ""){
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_3);
            $("#J_popup_name").focus().addClass("login-txt-error");

            $("#J_popup_login").val(lang_header.login_6);
            return false;
        }else{
            $("#J_popup_name").removeClass("login-txt-error");
            $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
        }
        if(paw.replace(/\s*/, "") == "" || paw == lang_header.login_5){
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_4);
            $("#J_popup_paw_txt").focus();
            $("#J_popup_paw").addClass("login-txt-error");

            $("#J_popup_login").val(lang_header.login_6);
            return false;
        }else{
            $("#J_popup_paw").removeClass("login-txt-error");
            $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
        }

        if(salt == ""){
            $.ajax({
                url : "/login/get_salt/",
                type : "post",
                dataType : "json",
                async: false,
                data : {username : username, code : code},
                success : function(data){
                    if(data.state == 0){
                        $("#J_popup_login").val(lang_header.login_6);
                        $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.msg);
                        $("#J_popup_" + data.val_type).focus();
                        $("#J_popup_" + data.val_type).addClass("login-txt-error");
                        return false;
                    }else if(data.state == 1){
                        $("#J_popup_login").val(lang_header.login_6);
                        salt = data.data;
						reserved_salt = data.reserved_data;

                        $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
                        $("#J_popup_name").removeClass("login-txt-error");
                        return false;
                    }else{
                        $("#J_popup_login").val(lang_header.login_6);
                        $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html(data.msg);
                        $("#J_popup_" + data.val_type).focus();
                        $("#J_popup_" + data.val_type).addClass("login-txt-error");
                        return false;
                    }
                },
                error : function(jqXHR, textStatus, errorThrown){
                    if(typeof error_cb === "function"){
                        error_cb(jqXHR, textStatus, errorThrown);
                    }
                }
            });


        }

        if(salt == ""){
            return false;
        }

        var enc_paw = md5(paw + salt);
		var reserved_enc_paw = md5(paw + reserved_salt);

        $.ajax({
            url : "/login/login_f/",
            type : "post",
            dataType : "json",
            async: false,
            data : {code : code, username : username, password : enc_paw,reserved:reserved_enc_paw, is_remember : is_remember,current_position:login_position,current_page:current_page},
            success : function(data){
                if(data.state == 0){
                    if(data.msg == lang_header.login_tips_2){
                        $("#J_popup_login").val(lang_header.login_6);
                        $("#J_popup_code").focus();
                        $("#J_popup_code").addClass("login-txt-error");
                        $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.msg);
                        return false;
                    }else if(data.msg == HQ.config.language.commonUserTips[language][0] || data.msg == HQ.config.language.commonUserTips[language][1]){
                        $("#J_popup_login").val(lang_header.login_6);
                        $("#J_popup_name").focus();
                        $("#J_popup_name").addClass("login-txt-error");
                        $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.msg);
                        return false;
                    }else if(data.msg == lang_header.login_tips_5){
                        $("#J_popup_login").val(lang_header.login_6);
                        $("#J_popup_paw_txt").focus();
                        $("#J_popup_paw").addClass("login-txt-error");
                        $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.msg);
                        return false;
                    }
                }else {
                    if (data.messages_status == 1) {
                        //关闭登录窗口
                        $(".index-popup").fadeOut("slow");
                    } else {
                        $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html(data.msg);
                        is_login = 0;
                        window.location.reload(window.location.href);
                    }
                }
            },
            error : function(jqXHR, textStatus, errorThrown){
                if(typeof error_cb === "function"){
                    error_cb(jqXHR, textStatus, errorThrown);
                }
            }
        });
    }

    $("#J_logout").click(function(){
        $.ajax({
            url : "/login/logout/",
            type : "post",
            dataType : "json",
            async: false,
            data : {},
            success : function(data){

                is_login = 0;
                $("#J_header_login").val(lang_header.login_6);
                window.location.reload(window.location.href);
            },
            error : function(jqXHR, textStatus, errorThrown){
                if(typeof error_cb === "function"){
                    error_cb(jqXHR, textStatus, errorThrown);
                }
            }
        });
    });
    $("#J_popup_mobile").focus(function(){
        var mobile = $(this).val();
        if(mobile == "" || mobile == lang_header.login_13){
            $(this).val("");
        }
        $(this).css("color","#333");

    });
    $("#J_popup_mobile").blur(function(){
        var mobile = $(this).val();
        if(!(/^(1)[0-9]{10}$/.test(mobile))) {
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_16);
            $("#J_popup_mobile").addClass("Validform_error");
            return false;
        }else{
            $("#J_popup_error_msg").html("");
            $("#J_popup_mobile").removeClass("Validform_error");
            HQ.ajax("/personal/login/mobile_check/", {mobile:mobile}, function(data){
                if(data.status == 'y'){
                    $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
                } else {
                    $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.info);
                }
            }, function(){}, "POST");
            return false;
        }

    }).keyup(function(ev){
        var val = $(this).val();
        if(ev.keyCode == 13 && is_login != 1 && val.replace(/\s*/, "") != ""){
            pop_mobile_check_login();
        }
    });
    $("#J_popup_mcode").focus(function(){
        var mcode = $(this).val();
        if(mcode == "" || mcode == lang_header.login_14){
            $(this).val("");
        }
        $(this).css("color","#333");

    }).keyup(function(ev){
        var val = $(this).val();
        if(ev.keyCode == 13 && is_login != 1 && val.replace(/\s*/, "") != ""){
            pop_mobile_check_login();
        }
    });
    $("#J_popup_mcode").blur(function(){
        var mcode = $(this).val();
        var mobile = $("#J_popup_mobile").val();
        if(mcode == "") {
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_14);
            $("#J_popup_mcode").addClass("Validform_error");
            return false;
        }else{
            $("#J_popup_error_msg").html("");
            $("#J_popup_mcode").removeClass("Validform_error");
            HQ.ajax("/personal/login/actcode_verify/", {mobile:mobile,actcode:mcode}, function(data){
                if(data.status == 'y'){
                    $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
                } else {
                    $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.info);
                }
            }, function(){}, "POST");
            return false;
        }

    });

    $("#J_popup_send_sms").click(function(){
        if($(this).hasClass("account-f-getcode-no")){
            return false;
        }

        if(get_cookie('r_m_wait_time')) {
            return false;
        }
        var mobile = $("#J_popup_mobile").val();

        if(!(/^(1)[0-9]{10}$/.test(mobile))) {
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_16);
            $("#J_popup_mobile").addClass("Validform_error");
            return false;
        }

        HQ.ajax("/personal/login/send_sms/", {mobile:mobile}, function(data){
            if(data.status == 'y'){
                $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html(data.info);
                wait_time($("#J_popup_send_sms"),59,'r_m_',HQ.config.language.commonUserTips[language][2]);
            } else {
                $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.info);
            }
        }, function(){}, "POST");
    });
    $("#J_popup_mobile_login_btn").click(function(){
        pop_mobile_check_login();
    });

    function pop_mobile_check_login(){
        if(get_cookie('r_m_wait_time')) {
            return false;
        }
        var mcode = $("#J_popup_mcode").val();
        var mobile = $("#J_popup_mobile").val();
        var login_position = $(".account-btn").attr("data-position");
        if($('input[name="cookietime"]').is(":checked")){
            var cookietime = 1;
        }else{
            var cookietime = 0;
        }

        if(!(/^(1)[0-9]{10}$/.test(mobile))) {
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_16);
            $("#J_popup_mobile").addClass("Validform_error");
            return false;
        }

        if(mcode == "" || mcode == lang_header.login_14) {
            $("#J_popup_mcode").addClass("Validform_error");
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_14);
            return false;
        }
        HQ.ajax("/personal/login/do_mobile_login/", {mobile:mobile,mcode:mcode,cookietime:cookietime,current_position:login_position,current_page:current_page}, function(data){
            if(data.status == 'y'){
                $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html(data.info);
                window.setTimeout(function(){
                    window.location.reload(window.location.href);
                },500);
            } else {
                $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.info);
            }
        }, function(){}, "POST");
    }

    $("#J_popup_account").focus(function(){
        var account = $(this).val();
        if(account == "" || account == lang_header.login_16){
            $(this).val("");
        }
        $(this).css("color","#333");

    });
    $("#J_popup_account").blur(function(){
        var account = $(this).val();
        if(account == "") {
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_18);
            $("#J_popup_account").addClass("Validform_error");
            return false;
        }else{
            $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
            $("#J_popup_account").removeClass("Validform_error");
            return false;
        }

    }).keyup(function(ev){
        var val = $(this).val();
        if(ev.keyCode == 13 && is_login != 1 && val.replace(/\s*/, "") != ""){
            pop_check_account_login();
        }
    });
    $("#J_popup_account_paw_txt").focus(function(){
        var val = $(this).val();
        $("#J_popup_account_paw").show().focus();
        $(this).hide();
    });
    $("#J_popup_account_paw").focus(function(){
        var paw = $(this).val();
        if(paw == "" || paw == lang_header.login_5){
            $(this).val("");
        }
        $(this).css("color","#333");

    });
    $("#J_popup_account_paw").blur(function(){
        var paw = $(this).val();
        if(paw == "") {
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_4);
            $("#J_popup_account_paw").addClass("Validform_error");
            return false;
        }else{
            $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
            $("#J_popup_account_paw").removeClass("Validform_error");
            return false;
        }

    }).keyup(function(ev){
        var val = $(this).val();
        if(ev.keyCode == 13 && is_login != 1 && val.replace(/\s*/, "") != ""){
            pop_check_account_login();
        }
    });
    $("#J_popup_account_vcode").focus(function(){
        var vcode = $(this).val();
        if(vcode == "" || vcode == lang_header.login_14){
            $(this).val("");
        }
        $(this).css("color","#333");

    });
    $("#J_popup_account_vcode").blur(function(){
        var vcode = $(this).val();
        if(vcode == "") {
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_14);
            $("#J_popup_account_vcode").addClass("Validform_error");
            return false;
        }else{
            $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
            $("#J_popup_account_vcode").removeClass("Validform_error");
            return false;
        }

    }).keyup(function(ev){
        var val = $(this).val();
        if(ev.keyCode == 13 && is_login != 1 && val.replace(/\s*/, "") != ""){
            pop_check_account_login();
        }
    });
    $("#J_popup_account_login").click(function(){
        pop_check_account_login();
    });

    function pop_check_account_login(){
        if(get_cookie('r_m_wait_time')) {
            return false;
        }
        var vcode = $("#J_popup_account_vcode").val();
        var paw = $("#J_popup_account_paw").val();
        var account = $("#J_popup_account").val();
        var login_position = $(".account-btn").attr("data-position");
        if($('input[name="cookietime"]').is(":checked")){
            var cookietime = 1;
        }else{
            var cookietime = 0;
        }

        if(account == "" || account == lang_header.login_16) {
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_18);
            $("#J_popup_account").blur().addClass("Validform_error");
            return false;
        }else{
            $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
            $("#J_popup_account").removeClass("Validform_error");
        }
        if(paw == "" || paw == lang_header.login_5) {
            $("#J_popup_account_paw").blur().addClass("Validform_error");
            $("#J_popup_account_paw_txt").blur().addClass("Validform_error");
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_4);
            return false;
        }else{
            $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
            $("#J_popup_account_paw").removeClass("Validform_error");
        }
        if(vcode == "" || vcode == lang_header.login_14) {
            $("#J_popup_account_vcode").blur().addClass("Validform_error");
            $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(lang_header.login_tips_14);
            return false;
        }else{
            $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
            $("#J_popup_account_vcode").removeClass("Validform_error");
        }

        if(salt == ""){
            $.ajax({
                url : "/personal/login/get_salt/",
                type : "post",
                dataType : "json",
                async: false,
                data : {username : account},
                success : function(data){
                    if(data.state == 0){
                        $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.msg);
                        $("#J_popup_" + data.val_type).focus();
                        $("#J_popup_" + data.val_type).addClass("login-txt-error");
                        return false;
                    }else if(data.state == 1){
                        salt = data.data;
						reserved_salt = data.reserved_data;

                        $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html("");
                        $("#J_popup_name").removeClass("login-txt-error");
                        return false;
                    }else{
                        $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.msg);
                        $("#J_popup_" + data.val_type).focus();
                        $("#J_popup_" + data.val_type).addClass("login-txt-error");
                        return false;
                    }
                },
                error : function(jqXHR, textStatus, errorThrown){
                    if(typeof error_cb === "function"){
                        error_cb(jqXHR, textStatus, errorThrown);
                    }
                }
            });
        }

        if(salt == ""){
            return false;
        }
        var enc_paw = md5(paw + salt);
		var reserved_enc_paw = md5(paw + reserved_salt);
		
        HQ.ajax("/personal/login/do_login/", {account:account,password:enc_paw,reserved:reserved_enc_paw,vcode:vcode,cookietime:cookietime,current_position:login_position,current_page:current_page}, function(data){
            if(data.status == 'y'){
                $("#J_popup_error_msg").removeClass("Validform_wrong").addClass("Validform_right").html(data.info);
                window.setTimeout(function(){
                    window.location.reload(window.location.href);
                },500);
            } else {
                $("#J_popup_error_msg").removeClass("Validform_right").addClass("Validform_wrong").html(data.info);
            }
        }, function(){}, "POST");
    }

    //刷新验证码
    $("#J_popup_verify_code").click(function(){
        $("#J_popup_verify_code").attr('src', '/verify_code?t='+(new Date().getTime()));
    });

    $(document).click(function(){
        $("#J_sea_box").hide();
    });

    //发送激活码
    if(get_cookie('r_e_wait_time')) {
        wait_time($("#J_popup_send_sms"),get_cookie('r_e_wait_time'),'r_e_',HQ.config.language.com_errorSendEmail[language][1]);
    }
    if(get_cookie('r_m_wait_time')) {
        wait_time($("#J_popup_send_sms"),get_cookie('r_m_wait_time'),'r_m_',HQ.config.language.com_errorSendEmail[language][2]);
    }
    $('#J_popup_newyear_btn').live('click', function(){
        window.location.reload(window.location.href);
    });

	var dist_id = 0;
	var dist_user_id = 0;
	var uname = "";
	var realname = "";
	var partnerId = "";
	/*$.ajax({
		type:"post", 
		url:"/ajax/get_dist_info", 
		data: {}, 
		dataType: 'json', 
		async : false,
		success:function(data){
            // 处理分销商信息
			if(data.res == 0){
				dist_id = data.msg.dist_id;
				dist_user_id = data.msg.dist_user_id;
				uname = data.msg.distributors_cn_name + " " + data.msg.account;
				realname = data.msg.distributors + " " + data.msg.account;
				partnerId = data.msg.distributors + data.msg.account;
				dist_lang = data.msg.language;
                var url = window.location.href;
                if(url.indexOf('cashier') > -1 || url.indexOf('thotelorder') > -1) return false;
				handle_socket_msg(dist_user_id+','+dist_id+','+dist_lang);
			}else{
				partnerId = data.msg.partnerId;
				if(zhiManager){
                    zhiManager.set('groupId','3d511f88a1e54fe2ad69bce145113588');
                }

			}
		}
	});*/

    // 处理登陆cookie
    var distributor_code    = $.cookie("distributor_code");
    var distributor_account = $.cookie("distributor_account");
    var is_remember         = $.cookie("is_remember");
    if(is_remember == 1){
        $("#J_header_code").val(distributor_code).css("color","#333");
        $("#J_header_name").val(distributor_account).css("color","#333");
        $("#J_is_remember").attr("checked","checked");
        $("#J_popup_code").val(distributor_code).css("color","#333");
        $("#J_popup_name").val(distributor_account).css("color","#333");
        $("#J_popup_is_remember").attr("checked","checked");
    }

    // 根据屏幕高度设置（儿童年龄、国籍列表）下拉框高度
    var win = $(window),
        winHeight = win.innerHeight(),
        childAgeList = $('.d-room-peo-child-age-list'),
        nationalityList = $('.J_room_sel_win_nationality');

    if(winHeight <= 600) {
        childAgeList.addClass('h216');
        nationalityList.addClass('h400');
    } else if (winHeight > 600 && winHeight <= 768) {
        childAgeList.addClass('h288');
        nationalityList.addClass('h500');
    } else {
        childAgeList.addClass('h360');
        nationalityList.addClass('h576');
    }

    // ------------ footer start ------------ //
    // 侧边栏-> 滚动 -> 返回顶部(显示/隐藏)
    var return_top_obj = $("#J_return_top");
    var side_right_obj = $("#J_side_right");
    var is_show_top = 0;
    win.scroll(function(){
        var top = win.scrollTop();
        if(top > 300 && is_show_top == 0){
            return_top_obj.fadeIn(200).css({display: 'block'});
            is_show_top = 1;
        }else if(top < 300 && is_show_top == 1){
            return_top_obj.fadeOut(200);
            is_show_top = 0;
        }
    });

    // 侧边栏-> 缩放 -> 调整侧边栏距窗口右侧位置
    side_right_obj.css({right : win.width() <= 1300 ? 10 : (win.width() - 1250)/2 - 66}).show();
    win.resize(function(){
        if(win.width() <= 1300){
            side_right_obj.css('right','10px');
        }else{
            side_right_obj.css({right : (win.width() - 1250)/2 - 66});
        }
    });

    // 百度统计接口 -> 为每个页面添加push.js
    var bp = document.createElement('script');
    bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);

    // 消息提示确认 -> 刷新当前页面 (没找到)
    $("#J_popup_messages_btn").click(function(){
        window.location.reload(window.location.href);
    });

    // 合作伙伴跳转
    var href = window.location.href;
    var arr = href.split('/?');
    var arr1 = href.split('/');
    var arr_html = href.split('.html');
    var str = arr1[arr1.length-1];

    if(arr.length == 1 && arr_html.length == 1 && str != '' ){
        var hrefs = $("#partners").attr("href");
        $("#partners").attr("href", hrefs + "#partners") ;
    }
    // ------------ footer end ------------ //




	//zhichi
	var zhiManager = (getzhiSDKInstance());
	zhiManager.on("load", function() {zhiManager.initBtnDOM();});
	zhiManager.set('customMargin', 20);
	var inventorySearch = $.cookie("sea_arr");
    if(inventorySearch){
        inventorySearch = eval("("+inventorySearch+")");
        zhiManager.set("partnerId",partnerId);
        zhiManager.set("uname",uname);
        zhiManager.set("realname",realname);
        zhiManager.set("params",JSON.stringify({
            'dist_id':dist_id,
            'dist_user_id':dist_user_id,
            'checkin':inventorySearch.checkin,
            'checkout': inventorySearch.checkout,
            'room':inventorySearch.room,
            'adult': inventorySearch.adult,
            'child': inventorySearch.child,
            'child_age': inventorySearch.child_age,
            'url': window.location.href
        }));
    }
})


// 消息推送部分开始
// 用户登陆时开启长连接监听实时消息
function handle_socket_msg (s_data){
    var data_arr = s_data.split(',');
    window.onload = function () {
        var url = window.location.href;
        if(url.indexOf('cashier') > -1 || url.indexOf('thotelorder') > -1) return false;
        $('head').append('<script type="text/javascript" src="/static/socket/js/swfobject.js"></script><script type="text/javascript" src="/static/socket/js/web_socket.js"></script>');
        // 处理显示新消息
        var show_msg = function (data) {
            if (data.length <= 0) return;
            if ($(".msg-center").length === 0) {
                $("body").append('<div class="msg-center"><ul></ul></div>');
            }

            var msg_html = '';
            var icon_str = data[0].type_name ? (data[0].show_type == 2 ? 'strong-news' : 'weak-news') : data[0].icon;
            var msg_title = (data[0].type_name !== "" ? "【"+data[0].type_name+"】" : "") + data[0].title;
            msg_html += '<li class="hide" data-icon="'+icon_str+'" data-href="' + data[0].jump_url + '" data-id="' + data[0].id + '"><i class="msg-close-icon"></i><div class="msg-body">';
            msg_html += '<div class="msg-'+icon_str+'-icon msg-title">'+ msg_title +'</div>';
            msg_html += '<div class="msg-con">'+data[0].contents+'</div>';
            if (icon_str !== 'strong-news' && icon_str !== 'weak-news') {
                msg_html += '<div class="msg-con">'+data[0].checkinout+'<span class="fr">'+data[0].amount+'</span></div>';
            }
            msg_html += '</div></li>';

            var html_obj = $(msg_html);
            $(".msg-center ul").append(html_obj);
            if (icon_str === 'weak-news') {
                $.ajax({
                    type: 'POST',
                    url: '/message/mark_weak_msg',
                    data: {msg_id: data[0].id},
                    dataType: 'json',
                    success: function (data) {},
                    error: function (e) {}
                });
            }
            setTimeout(function () {
                $(html_obj).fadeOut(1500).delay(2000).remove();
            }, 6000);
            fadeInMsg();
        };
        var fadeInMsg = function () {
            $('.msg-center ul li').each(function (i,v) {
                if ($(v).hasClass('hide')) {
                    $(v).slideDown();
                }
            });
            $(".msg-center").css({right : $(window).width() <= 1300 ? 70 : ($(window).width() - 1250)/2});
            $(window).resize(function(){
                if($(window).width() <= 1300){
                    $(".msg-center").css('right','70px');
                }else{
                    $(".msg-center").css({right : ($(window).width() - 1250)/2});
                }
            });
            if(window.location.href.indexOf('admin') > -1 || window.location.href.indexOf('hotelmaps') > -1) {
                $(".msg-center").css('right', '2%');
            }
        };

        // 获取用户消息
        var get_user_msg = function () {
            $.ajax({
                type: 'POST',
                url: '/message/get_user_msg',
                data: {
                    lang:language
                },
                dataType: 'json',
                success: function (data) {
                    if (data.length) {
                        show_msg(data);
                    }
                },
                error: function (e) {
                }
            });
        };
        // 绑定客户端
        var bind_client = function (c_id) {
            $.ajax({
                type: "POST",
                url : "/message/bind_client",
                data: {c_id: c_id},
                dataType: "json",
                success: function (res) {
                    if (res.status == 2) {
                        window.location.reload(true);
                    }
                }
            });
        };

        // 如果浏览器不支持websocket，会使用这个flash自动模拟websocket协议
        WEB_SOCKET_SWF_LOCATION = "/static/socket/swf/WebSocketMain.swf";
        WEB_SOCKET_DEBUG = false;
        var ws, intervalobj, ws_connect;
        // 连接服务端
        ws_connect = function () {
            ws = new WebSocket(HQ.config.msg_push_url);
            ws.onopen = function () {};
            ws.onmessage = function (e) {
                var data = JSON.parse(e.data);
                switch (data['type']) {
                    case 'init':
                        // 初始化绑定
                        bind_client(data['client_id']);
                        clearInterval(intervalobj);
                        intervalobj = setInterval(doping, 45000);
                        break;
                    case 'new':
                        // 新消息
                        show_msg([data]);
                        break;
                    case 'close':
                        // 关闭消息框
                        break;
                    case 'error':
                        ws.close();
                        clearInterval(intervalobj);
                        break;
                }
            };
            ws.onclose = function () {
                re_connect();
            };
            ws.onerror = function () {
                re_connect();
            };
        };
        ws_connect();
        // 发送心跳，保持长连接
        function doping() {
            ws.send('{"type":"ping"}');
        }
        // 断开重连
        var re_connect_times = 0;
        function re_connect () {
            clearInterval(intervalobj);
            if (++re_connect_times <= 5) {
                // 延后20s重连
                setTimeout(ws_connect(), 20000);
            } else {
                ajax_get_msg();
            }
        }
        // ajax轮询获取用户最新消息
        function ajax_get_msg() {}
        // 关闭消息
        $(document).on("click", ".msg-center .msg-close-icon", function (e) {
            e.stopPropagation();
            $(this).parent().fadeOut('slow').delay(1000).remove();
        });
        // 消息点击
        $(document).on("click", ".msg-center li", function (e) {
            e.stopPropagation();
            var jump_url = $(this).data('href');
            var op_id = $(this).data('id');
            var icon_str = $(this).attr('data-icon');
            if (icon_str == 'strong-news' || icon_str == 'weak-news') {
                $.ajax({
                    type: 'POST',
                    url:'/message/operation_msg',
                    data:{id:op_id},
                    dataType: 'json',
                    success: function (data) {},
                    error: function () {}
                });
            }

            if (jump_url) {
                window.location.href = jump_url;
            }
        });
        get_user_msg(true);
    }
}

// 消息推送部分结束